package unit01;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangzhuangzhuang
 * @DESC: 字符串排序
 * @date 2022-05-03 15:16
 */
public class StringSortionDemo {

	public static void main(String[] args) {
//		naturalOrderDemo();
//		reverseOrderDemo();
//		lowerCaseOrderDemo();
		orderByNameTheByNaturalOrder();
	}

	/**
	 * 自然排序：按照字母顺序（ASCII码 有小到大A -> z）
	 */
	public static void naturalOrderDemo() {
		List<String> strings = Arrays.asList("Connery", "Lazenby", "Moore", "Dalton", "Brosnan", "craig");
		List<String> sortedList = strings.stream()
				.sorted(Comparator.naturalOrder())
				.collect(Collectors.toList());
		sortedList.forEach(System.out::println);
	}

	/**
	 * 反向排序：按照字母反向排序（z -> A）
	 */
	public static void reverseOrderDemo() {
		List<String> strings = Arrays.asList("Connery", "Lazenby", "Moore", "Dalton", "Brosnan", "craig");
		List<String> sortedList = strings.stream()
				.sorted(Comparator.reverseOrder())
				.collect(Collectors.toList());
		sortedList.forEach(System.out::println);
	}

	/**
	 * 按小写名称排序
	 */
	public static void lowerCaseOrderDemo() {
		List<String> strings = Arrays.asList("Connery", "Lazenby", "Moore", "Dalton", "Brosnan", "craig");
		List<String> sortedList = strings.stream()
				.sorted(Comparator.comparing(String::toLowerCase))
				.collect(Collectors.toList());
		sortedList.forEach(System.out::println);
	}

	/**
	 * 按照姓名长度排序
	 */
	public static void orderByNameLength() {
		List<String> strings = Arrays.asList("Connery", "Lazenby", "Moore", "Dalton", "Brosnan", "craig");
		List<String> sortedList = strings.stream()
				.sorted(Comparator.comparing(String::length))
				.collect(Collectors.toList());
		sortedList.forEach(System.out::println);
	}

	/**
	 * 先按照姓名长度排序，若姓名长度相同则按照字典序排序
	 */
	public static void orderByNameTheByNaturalOrder() {
		List<String> strings = Arrays.asList("Connery", "Lazenby", "Moore", "Dalton", "Brosnan", "craig");
		List<String> sortedList = strings.stream()
				.sorted(Comparator.comparing(String::length))
				.collect(Collectors.toList());
		sortedList.forEach(System.out::println);
	}
}